package com.zzy.springboot_security.service.impl;

import com.zzy.springboot_security.entity.User;
import com.zzy.springboot_security.mapper.UserMapper;
import com.zzy.springboot_security.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhuZhengYang
 * @description TODO
 * @since 2022/1/20
 */
@Service
public class UserServiceImpl<T extends User> implements UserDetailsService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.findUser(username);
        if (user==null){
            throw new UsernameNotFoundException("不存在该用户！");
        }
        List<SimpleGrantedAuthority> authorities=new ArrayList<>();
        String role =user.getRole();
        if(role!=null){
            authorities.add(new SimpleGrantedAuthority(role.trim()));
        }


        return new org.springframework.security.core.userdetails.User(user.getName(),user.getPassword(),authorities);

    }
}
